package com.leo.manage_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.Advertise;
import com.leo.manage_system.entity.Res;
import com.leo.manage_system.entity.UserWechat;
import com.leo.manage_system.mapper.AdminMapper;
import com.leo.manage_system.mapper.AdvertiseMapper;
import com.leo.manage_system.service.AdvertiseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 广告 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-28
 */
@Service
public class AdvertiseServiceImpl extends ServiceImpl<AdvertiseMapper, Advertise> implements AdvertiseService {

    @Autowired
    private AdvertiseMapper advertiseMapper;

    @Override
    public boolean addAdvertise(Advertise advertise) {
        try{
            advertiseMapper.insert(advertise);
        }catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteAdvertise(String id) {
        try{
            advertiseMapper.deleteById(id);
        }catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public Map<String, Object> getAdvertisePageQuery(Page<Advertise> pageParam) {
        QueryWrapper<Advertise> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("id","url","click_time","DATE_FORMAT(expiration_time,'%Y-%m-%d')","gmt_create","gmt_modify","is_deleted","version");
        queryWrapper.orderByDesc("gmt_create");
        advertiseMapper.selectPage(pageParam,queryWrapper);
        System.out.println(pageParam.getRecords());
        List<Advertise> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();

        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

    @Override
    public List<String> getAdvertise() {
        QueryWrapper<Advertise> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("url");
        List<String> list = new ArrayList<>();
        List<Advertise> list2 = advertiseMapper.selectList(queryWrapper);
        for (int i = 0 ;i<list2.size();i++){
            list.add(list2.get(i).getUrl());

        }
        return list;
    }


}
